package com.erp.module.ProjectOrderProcess.ProjectOrderProcessCreate

import android.app.Activity
import android.content.Intent
import android.util.Log
import android.view.View
import androidx.appcompat.app.AlertDialog
import com.erp.CaptureActivity
import com.erp.R
import com.erp.activity.BaseActivity
import com.erp.module.ProjectOrderProcess.ProjectOrderProcessContract
import com.erp.module.ProjectOrderProcess.ProjectOrderProcessP
import com.erp.permission.PermissionListener
import com.erp.util.Myutil
import com.erp.util.PermissionUtils
import kotlinx.android.synthetic.main.activity_project_order_process.*
import kotlinx.android.synthetic.main.header_knoya.*

class ProjectOrderProcessActivity : BaseActivity() , ProjectOrderProcessContract.ProjectOrderProcessPutView, View.OnClickListener {
    lateinit var mlist : MutableList<String>
    lateinit var KP : ProjectOrderProcessContract.ProjectOrderProcessKP
    lateinit var updialog: AlertDialog
    var code = 1
    override fun getLayoutId(): Int {
        return R.layout.activity_project_order_process
    }

    override fun initCreate() {
        code = intent.getIntExtra("code",code)
        mlist = ArrayList()
        KP = ProjectOrderProcessP(mlist,this ,this,code);
        btTijiao.setOnClickListener(this)
        ivScan.setOnClickListener(this)
        btChexiao.setOnClickListener(this)
        iv_header_left.visibility = View.VISIBLE
        iv_header_left.setOnClickListener(this)
        when(code){
            1 -> {
                tv_header_title.text = "开料扫描上传"
            }
            2 -> {
                tv_header_title.text = "封边扫描上传"
            }
            3 -> {
                tv_header_title.text = "打孔扫描上传"
            }
            4 -> {
                tv_header_title.text = "分包扫描上传"
            }
        }

    }

    override fun onClick(v: View?) {
        when(v){
            btTijiao -> {
                if(mlist.size == 0){
                    Myutil.MyTost("没有扫描，不能上传")
                    return
                }
                updialog = Myutil.showupdatedialog(this, "正在上传", false)
                KP.putProjectOrderProcess()
            }
            ivScan -> {
                PermissionUtils.issaomiao(this,object : PermissionListener {
                    override fun onSucceed(requestCode: Int, grantPermissions: MutableList<String>) {
                        val intent= Intent(this@ProjectOrderProcessActivity, CaptureActivity:: class.java)
                        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                        startActivityForResult(intent, 2)
                    }

                    override fun onFailed(requestCode: Int, deniedPermissions: MutableList<String>) {
                        Myutil.MyTost("没有权限，无法扫描")
                    }

                })
            }
            btChexiao -> {
                if(mlist.size > 0){
                    mlist.removeAt(mlist.size-1)
                }
                steScanData()
            }
            iv_header_left -> {
                finish()
            }
        }
    }

    /**获取扫描数据*/
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null||resultCode!= Activity.RESULT_OK){
            return
        }
        when (requestCode) {
            2 -> {
                val bundle = data.extras
                val s = bundle?.get("result") as String
                Log.d("onActivityResult",s)
                Log.d("onActivityResult",mlist.contains(s).toString())
                if(!mlist.contains(s)){
                    mlist.add(s)
                }
                steScanData()
            }
        }
    }

    fun steScanData(){
        var s = "";
        for (i in 0 until mlist.size){
            s = s + mlist[i]
            if(i != mlist.size-1){
                s = s + "\n"
            }
        }
        tvScandata.setText(s)
    }

    override fun putProjectOrderProcessOk() {
        Myutil.MyTost("数据上传成功")
        updialog.dismiss()
        mlist.clear()
        steScanData()
    }

    override fun putProjectOrderProcessNo() {
        updialog.dismiss()
    }
}